home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 15648 < prev    next >
Encoding:
Text File  |  1996-08-05  |  2.1 KB  |  78 lines

  1. Path: nntp.teleport.com!usenet
  2. From: GHouck <hksys@teleport.com>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Q: Newbie using text files
  5. Date: 20 Apr 1996 20:14:35 GMT
  6. Organization: systems hk
  7. Message-ID: <4lbgjb$l8u@nadine.teleport.com>
  8. References: <4l6aqu$rfr@chile.it.earthlink.net>
  9. NNTP-Posting-Host: ip-pdx03-10.teleport.com
  10. Mime-Version: 1.0
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: 7bit
  13. X-Mailer: Mozilla 1.22 (Windows; I; 32bit)
  14.  
  15. brunop@earthlink.net (Peter Bruno) wrote:
  16. >I'm new to C programming so bear with me for a bit...
  17. >
  18. >I do a lot of small programming projects at work, mostly to make my
  19. >life easier, and I've been using QBasic.  I would like, however, to
  20. >use a betting langauage if I could.  But, trying to input and
  21. >processes text files in C seems a lot harder than in C, what with
  22. >pointers and all.
  23. >
  24. >My point, and I do have one, is it worth my time to learn C to process
  25. >ASCII text files with fixed lenth fields, for sorting etc...
  26. >
  27. >and could you suggest some routines for doing this...  in basic I just
  28. >do the following:
  29. >
  30. >open "foo.bar" for input as #1
  31. >open "foo.out" for output as #2
  32.  
  33. >while not eof(1)
  34. >    line input #1, row
  35. >    FName = mid$(row, 1, 25)    'characters 1-25 are first name
  36. >    LName = mid$(row, 26, 50)
  37. >    test = mid$(LName, 26, 1)
  38. >    if test < "M" then print #2, row    'if person's last name begins with
  39. >wend
  40.  
  41. >close #1, #2
  42. >
  43. >can someone (kind-hearted and forgiving of stupid questions) please
  44. >offer suggestions on how to do this in C?  PLEASE!
  45. >
  46. Peter,
  47.  
  48. The following (without any error-trapping) is a pretty close
  49. analog to your BASIC code.  So close that it ought to convince
  50. you that you many of the rules and techniques are similar
  51. if you need them to be.
  52.  
  53. Yours, Geoff Houck
  54.  
  55. int    main( void )
  56. {
  57.   char    row[128], fname[64] lname[64];
  58.   FILE    *f1, *f2;
  59.  
  60.   f1 = fopen( "foo.bar","r" );
  61.   f2 = fopen( "foo.out","w" );
  62.  
  63.   while( fgets(row,sizeof(row),f1) ) {
  64.     strncpy( fname,row,25 );
  65.     fname[25] = '\0';  
  66.     strncpy( lname,(row+25),25 ); 
  67.     lname[25] = '\0';
  68.    
  69.     if( *lname < 'M' )
  70.       fprintf( f2,"%s",row );
  71.   }
  72.  
  73.   fclose( f1 );
  74.   fclose( f2 );
  75. }
  76.  
  77.  
  78.